package com.leyou.trade.web;

import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;

    @PostMapping
    public ResponseEntity<Void> saveCartItem(@RequestBody CartItem cartItem){
        cartService.saveCartItem(cartItem);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> addCartItem(@RequestParam("id") Long skuId, @RequestParam("num") Integer num){
        cartService.addCartItem(skuId, num);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCartItem(@PathVariable("id") Long skuId){
        cartService.deleteCartItem(skuId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<CartItem>> listCartItem(){
        return ResponseEntity.ok(cartService.listCartItem());
    }

}
